package cn.rocksolid.sport.security.service.impl;

import cn.rocksolid.sport.common.Constant.Symbol;
import cn.rocksolid.sport.security.dao.MenuDao;
import cn.rocksolid.sport.security.entity.MenuEntity;
import cn.rocksolid.sport.security.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * System Menu service implementation
 *
 * @author Axl Zhao
 * @email axl.zhao@163.com
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuDao, MenuEntity> implements MenuService {

  @Override
  public Set<String> queryPermsByUid(final String uid) {
    Set<String> perms = new HashSet<>();
    for (String perm : ListUtils.emptyIfNull(this.getBaseMapper().queryPermsByUid(uid))) {
      if (StringUtils.isBlank(perm)) {
        continue;
      }
      perms.addAll(Arrays.asList(perm.trim().split(Symbol.COMMA)));
    }
    return perms;
  }

  @Override
  public List<MenuEntity> queryMenusByUid(final String uid) {
    return ListUtils.emptyIfNull(this.getBaseMapper().queryMenusByUid(uid));
  }

  @Override
  public List<MenuEntity> queryAllMenus() {
    return this.getBaseMapper().queryAllMenus();
  }
}
